package com.runen.infinitemovement.model.api;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.base.BaseAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.AchievementEntity;
import com.runen.infinitemovement.model.entity.AllPathSectionEntity;
import com.runen.infinitemovement.model.entity.AnswerEntity;
import com.runen.infinitemovement.model.entity.AnswerProgressEntity;
import com.runen.infinitemovement.model.entity.ArticleListEntity;
import com.runen.infinitemovement.model.entity.CalculateSportgoalEntity;
import com.runen.infinitemovement.model.entity.ClockAllEntity;
import com.runen.infinitemovement.model.entity.ClockDetailsEntity;
import com.runen.infinitemovement.model.entity.CompanyHozomRankEntity;
import com.runen.infinitemovement.model.entity.CompanyProgressRankEntity;
import com.runen.infinitemovement.model.entity.HitCardEntity;
import com.runen.infinitemovement.model.entity.HomeBarrageEntity;
import com.runen.infinitemovement.model.entity.HomeEntity;
import com.runen.infinitemovement.model.entity.HomeEntityV2;
import com.runen.infinitemovement.model.entity.MyLongRoadEntity;
import com.runen.infinitemovement.model.entity.PathFixedPointDetailEntity;
import com.runen.infinitemovement.model.entity.PathSectionCompleteEntity;
import com.runen.infinitemovement.model.entity.PhatStoryEntity;
import com.runen.infinitemovement.model.entity.RankingEntity;
import com.runen.infinitemovement.model.entity.RetrunSaveSportsEntity;
import com.runen.infinitemovement.model.entity.RideRoteJsonEntity;
import com.runen.infinitemovement.model.entity.RoadLineEntity;
import com.runen.infinitemovement.model.entity.RouteDetailsEntity;
import com.runen.infinitemovement.model.entity.RouteEntity;
import com.runen.infinitemovement.model.entity.SportsRecordEntity;
import com.runen.infinitemovement.model.entity.SportsStopEntity;
import com.runen.infinitemovement.model.entity.StartChallengeEntity;
import com.runen.infinitemovement.model.entity.TeamRankEntity;
import com.runen.infinitemovement.model.entity.UserClockInfoEntity;
import com.runen.infinitemovement.model.entity.UserExchangeStepEntity;
import com.runen.infinitemovement.model.entity.WeLongMarchHomeEntity;
import com.runen.infinitemovement.model.enums.SPCompat;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.JsonUtils;
import com.runen.rxnetlibrary.utils.SharePreferenceUtils;
import com.runen.rxnetlibrary.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class ChallengeAPI extends BaseAPI {
    private static final String MAP_PATH_SECTION = "mapPathSection";
    private static final String MAP_PATH_SECTION_VERSION = "mapPathSectionVersion";

    /**
     * 首页信息
     *
     * @param listener
     */
    public void onHome(BaseResultCallback<HomeEntity> listener) {
        get(Constant.HOME_ACTION, null, listener);
    }

    /**
     * 我的长征路
     *
     * @param callback
     */
    public void myLongRoad(String string, BaseResultCallback<MyLongRoadEntity> callback) {
        get(Constant.MY_LONG_ROAD_ACTION + string, null, callback);
    }


    /**
     * 获取路书坐标
     *
     * @param pathUrl
     * @param listener
     */
    public void getRideRoteQuery(String pathUrl, BaseResultCallback<List<RideRoteJsonEntity>> listener) {
        pathGet(pathUrl, null, listener);
    }

    /**
     * 路线
     *
     * @param callback
     */
    public void getRoadLineInfo(BaseResultCallback<RoadLineEntity> callback) {
        get(Constant.LONG_MARCH_PHTA_ACTION, null, callback);
    }

    /**
     * 获取我的路线和钱不路线
     *
     * @param str
     * @param listener
     */
    public void routeList(String str, BaseResultCallback<List<RouteEntity>> listener) {
        get(TextUtils.equals(str, "ME") ? Constant.MY_ROUTE_LIST_ACTION : Constant.ROUTE_LIST_ACTION, null, listener);
    }

    /**
     * 切换路线
     *
     * @param
     * @param listener
     */
    public void routeChange(BaseResultCallback<List<RouteEntity>> listener) {
        get(Constant.ROUTE_CHANGE_ACTION, null, listener);
    }

    /**
     * 开始挑战，运动开始
     *
     * @param str
     * @param listener
     */
    public void onStartChallenge(String str, BaseResultCallback<StartChallengeEntity> listener) {
        postBody(Constant.START_CHALLENGE_ACTION, str, listener);
    }

    /**
     * 获取线路故事
     *
     * @param str
     * @param listener
     */
    public void onPhatStory(String str, BaseResultCallback<List<PhatStoryEntity>> listener) {
        get(Constant.PHAT_STORY_ACTION + str, null, listener);
    }

    /**
     * 获取文章列表信息
     *
     * @param str
     * @param listener
     */
    public void onAllArticle(String str, BaseResultCallback<ArticleListEntity> listener) {
        get(Constant.ALL_ARTICLE_LIST_ACTION + str, null, listener);
    }

    /**
     * 线路详情
     *
     * @param str
     * @param listener
     */
    public void pathDetails(String str, BaseResultCallback<RouteDetailsEntity> listener) {
        if (TextUtils.isEmpty(str)) {
            get(Constant.ROUTE_DETAILS_ACTION, null, listener);
        } else {
            get(Constant.ROUTE_DETAILS_ACTION + str, null, listener);
        }
    }

    /**
     * 用户挑战初始化页面数据信息
     *
     * @param str
     * @param listener
     */
    public void userInitStartChallenge(String str, BaseResultCallback<StartChallengeEntity> listener) {
        get(Constant.USER_INIT_START_CHALLENGE + str, null, listener);
    }

    /**
     * 保存运动记录
     *
     * @param str
     * @param listener
     */
    public void saveSportsRecord(String str, BaseResultCallback<RetrunSaveSportsEntity> listener) {
        postBody(Constant.SAVE_SPORTS_RECORD_ACTION, str, listener);
    }

    /**
     * 保存结速运动数据
     *
     * @param string
     * @param callback
     */
    public void saveStopSportsRecord(String string, BaseResultCallback<SportsStopEntity> callback) {
        postBody(Constant.SAVE_STOP_SPORTS_RECOED_ACTION, string, callback);
    }

    /**
     * 排名
     *
     * @param type
     * @param listener
     */
    public void ranking(String str, int type, BaseResultCallback<RankingEntity> listener) {
        get(type == 0 ? Constant.TODAY_RANK_ACTION + str : Constant.TOTAL_RANK_ACTION + str, null, listener);
    }

    /**
     * 获取全部打卡点
     *
     * @param callback
     */
    public void clockAll(String str, BaseResultCallback<List<ClockAllEntity>> callback) {
        get(Constant.CLOCK_ALL_LIST_ACTION + str, null, callback);
    }

    /**
     * 路线点详情
     *
     * @param str
     * @param callback
     */
    public void roadSectionDetails(String str, BaseResultCallback<ClockDetailsEntity> callback) {
        get(Constant.ROAD_SECTION_DETAILS_ACTION + str, null, callback);
    }

    /**
     * 用户打卡点信息
     *
     * @param str
     * @param callback
     */
    public void userClockInfo(String str, BaseResultCallback<List<UserClockInfoEntity>> callback) {
        get(Constant.USER_CLOCK_INFO_ACTION + str, null, callback);
    }

    /**
     * 获取公司火种排行
     *
     * @param str
     * @param callback
     */
    public void getCompanyRank(String str, BaseResultCallback<CompanyHozomRankEntity> callback) {
        get(Constant.GET_COMPANY_RANK_ACTION + str, null, callback);
    }

    /**
     * 选择路段
     *
     * @param stirng
     * @param callback
     */
    public void onCheckRoadSection(String stirng, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.CHECK_ROAD_ACTION, stirng, callback);
    }

    /**
     * 我们的公司主页信息
     *
     * @param callback
     */
    public void onWeLongMarchHome(BaseResultCallback<WeLongMarchHomeEntity> callback) {
        get(Constant.WE_LONG_MARCH_HOME_ACTION, null, callback);
    }

    /**
     * 贡献火种
     *
     * @param string
     * @param callback
     */
    public void onHozomTinder(String string, BaseResultCallback<BaseResponseEntity<?>> callback) {
        postBody(Constant.HOZOM_TINDER_ACTION, string, callback);
    }

    /**
     * 团队火种榜
     *
     * @param string
     * @param callback
     */
    public void teamHozomRank(String string, BaseResultCallback<TeamRankEntity> callback) {
        get(Constant.GET_TEAM_HOZOM_RANK_ACTION + string, null, callback);
    }

    /**
     * 团队运动次数榜
     *
     * @param string
     * @param callback
     */
    public void teamSportsTimes(String string, BaseResultCallback<TeamRankEntity> callback) {
        get(Constant.GET_TEAM_TIMES_RANK_AVTION + string, null, callback);
    }

    /**
     * 获取运动记录
     *
     * @param string
     * @param callback
     */
    public void sportsRecord(String string, BaseResultCallback<SportsRecordEntity> callback) {
        get(Constant.SPORTS_RECORD_ACTION + string, null, callback);
    }

    /**
     * 获取公司进度排行
     *
     * @param callback
     */
    public void companyProgressRank(String str, BaseResultCallback<CompanyProgressRankEntity> callback) {
        get(Constant.COMPANY_PROGRESS_RANK_ACTION + str, null, callback);
    }

    /**
     * 获取个人长征路的成就列表
     *
     * @param targetId
     * @param callback
     */
    //achievement/medalAchievedRecord/app/personalLongMarchAchievement/get
    public void getAchievement(String targetId, BaseResultCallback<AchievementEntity> callback) {
        get(Constant.GET_ACHIEVEMENT_ACTION + targetId, null, callback);
    }

    /**
     * 自由挑战
     *
     * @param callback
     */
    public void onFreedomChallenge(BaseResultCallback<BaseResponseEntity<?>> callback) {
        post(Constant.FREEDOM_CHALLENGE_ACTION, null, callback);
    }

    /**
     * 获取打卡数据
     *
     * @param callback
     */
    public void hitCardInfo(BaseResultCallback<HitCardEntity> callback) {
        get(Constant.HIT_CARD_INFO, null, callback);
    }

    /**
     * 生成打卡记录
     *
     * @param callback
     */
    public void saveHitCardRecord(BaseResultCallback<BaseResponseEntity<?>> callback) {
        post(Constant.SAVE_HIT_CARD_RECORD, null, callback);
    }

    /**
     * 计算运动目标返回多少天
     *
     * @param str
     * @param callback
     */
    public void calculateSportGoal(String str, BaseResultCallback<CalculateSportgoalEntity> callback) {
        postBody(Constant.CALCULATE_SPORT_GOAL, str, callback);
    }

    /**
     * 首页弹幕数据
     *
     * @param callback
     */
    public void homeBarrage(BaseResultCallback<HomeBarrageEntity> callback) {
        get(Constant.HOME_BARRAGE, null, callback);
    }

    /**
     * 获取固定点详情
     *
     * @param params
     * @param callback
     */
    public void pathFixedPointDetail(Map<String, Object> params, BaseResultCallback<PathFixedPointDetailEntity> callback) {
        get(Constant.PATH_PATH_FIXED_POINT_DETAIL, params, callback);
    }

    /**
     * 获取用户步数公里数
     *
     * @param callback
     */
    public void userExchangeStep(BaseResultCallback<UserExchangeStepEntity> callback) {
        get(Constant.USER_EXCHANGE_STEP, null, callback);
    }

    /**
     * 获取地图数据版本号
     *
     * @param callback
     */
    public void getPathSectionVersion(BaseResultCallback<BaseResponseEntity> callback) {
        get(Constant.GET_PATH_SECTION_VERSION_NUMBER, null, callback);
    }

    /**
     * 获取地图数据
     *
     * @param callback
     */
    public void getPathSectionLatAndLon(BaseResultCallback<AllPathSectionEntity> callback) {
        get(Constant.GET_PATH_SECTION_LAT_LON, null, callback);
    }

    /**
     * 保存地图路段数据
     *
     * @return
     */
    public static void saveMapPathSection(List<AllPathSectionEntity.DataBean> datas) {
        SharePreferenceUtils.putString(MAP_PATH_SECTION, Utils.getApp(), SPCompat.MAP_PATH_SECTION, JsonUtils.toJson(datas));
    }

    /**
     * 获取地图路段数据
     */
    public static List<AllPathSectionEntity.DataBean> getMapPathSection() {
        String jsonStr = SharePreferenceUtils.getString(MAP_PATH_SECTION, Utils.getApp(), SPCompat.MAP_PATH_SECTION);
        return JsonUtils.fromJson(jsonStr, new TypeToken<ArrayList<AllPathSectionEntity.DataBean>>() {
        }.getType());
    }

    /**
     * 保存地图路段数据版本
     *
     * @return
     */
    public static void saveMapPathSectionVersion(String version) {
        SharePreferenceUtils.putString(MAP_PATH_SECTION_VERSION, Utils.getApp(), SPCompat.MAP_PATH_SECTION_VERSION, version);
    }

    /**
     * 获取地图路段数据版本
     */
    public static String getMapPathSectionVersion() {
        String jsonStr = SharePreferenceUtils.getString(MAP_PATH_SECTION_VERSION, Utils.getApp(), SPCompat.MAP_PATH_SECTION_VERSION);
        return JsonUtils.fromJson(jsonStr, String.class);
    }

    /**
     * 获取我的长征路每日问题列表
     */
    public void getPathAnswerList(BaseResultCallback<AnswerEntity> callback) {
        get(Constant.GET_PATH_PATHSECTION_ANSWER_LIST, null, callback);
    }

    /**
     * 长征路答题进度更新
     *
     * @param params
     * @param callback
     */
    public void postPathPathSectionAnswerProgress(String params, BaseResultCallback<AnswerProgressEntity> callback) {
        postBody(Constant.POST_PATH_PATHSECTION_ANSWER_PROGRESS_UPDATE, params, callback);
    }

    /**
     * 路段完成详情弹窗
     *
     * @param params
     * @param callback
     */
    public void getPathPathSectionCompletePopup(Map<String, Object> params, BaseResultCallback<PathSectionCompleteEntity> callback) {
        get(Constant.GET_PATH_PATHSECTION_COMPLETE_POPUP, params, callback);
    }

    /**
     * 首页信息
     *
     * @param listener
     */
    public void onHomeV2(BaseResultCallback<HomeEntityV2> listener) {
        get(Constant.GET_HOME_PAGE_INFO_2, null, listener);
    }
}
